package com.application.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;

import ReadWriteXML.CNeighbor;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.application.Adapter.NeighborAdapter;
import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.RowDTO.NeighborRowDTO;

public class frmDanhSachOnline extends Activity {
	//public static int ON_EXIT_APP=1111;
	ListView lvDanhSachThanhVien;
	TextView txtLoiNhan;
	ArrayList<NeighborRowDTO> data;
	Button btnMoiVaoNhom;
	String groupName = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_danh_sach_online);
		lvDanhSachThanhVien = (ListView) findViewById(R.id.lvDanhSachThanhVien);
		btnMoiVaoNhom = (Button) findViewById(R.id.btnMoiVaoNhom);
		lvDanhSachThanhVien.setChoiceMode(2);
		txtLoiNhan=(TextView)findViewById(R.id.txtLoiNhan);
		fillListView();
		registerForContextMenu(lvDanhSachThanhVien);
		btnMoiVaoNhom.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			
				for(int i=0;i<data.size();i++){
					if(data.get(i).checked){
						NetworkController.sendGroupInvite(new Address(data.get(i).neighbor.get_member().get_ip()),
							NetworkController._peer.getAccount().get_Username()+"#~"+groupName, txtLoiNhan.getText().toString());
					}
				}				
				finish();
			}
		});
		// Lấy tên nhóm từ activity trước
		Intent i = getIntent();
		if (i != null) {
			if (i.getStringExtra(frmTaoNhom.CODE_GROUP_NAME) != null)
				groupName = i.getStringExtra(frmTaoNhom.CODE_GROUP_NAME);
		}
	}

	private void fillListView() {

		data = new ArrayList<NeighborRowDTO>();
		ArrayList<CNeighbor> listNeighbor = ReadWriteFileController.readNeighbor();
		for (CNeighbor neighbor : listNeighbor) {
			NeighborRowDTO row = new NeighborRowDTO();
			row.checked = false;
			row.neighbor = neighbor;
			data.add(row);
		}
		NeighborAdapter adapter=new NeighborAdapter(this, data);
		lvDanhSachThanhVien.setAdapter(adapter);
	}
	@Override
	public void onStart(){
		super.onStart();
		ActivityController.setPeerActivity(this);
		ActivityController.addActivityToArray(this);
	}
	@Override
	public void onResume(){
		super.onResume();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onPause(){
		super.onPause();
		ActivityController.setPeerActivity(this);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("Phu","on onActivityResult ds online");
//		 if (resultCode == ON_EXIT_APP) {
//			 finish();
//		 }
	}
}
